package com.base.sms.repository.factory;

import com.base.sms.repository.VerificationCodeRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VerificationCodeRepositoryFactory implements RepositoryFactory{

    private final Logger log = LoggerFactory.getLogger(VerificationCodeRepositoryFactory.class);
    private static final Map<String, VerificationCodeRepository> verifyCodeMap = new HashMap<>();
    // 构造函数，如果是集合接口对象，那么会把spring容器中所有关于该接口的子类，全部抓出来放入到集合中
    public VerificationCodeRepositoryFactory(List<VerificationCodeRepository> strategyList){
        for (VerificationCodeRepository strategy: strategyList) {
            verifyCodeMap.put(strategy.support().toLowerCase(),strategy);
        }
    }

    @Override
    public VerificationCodeRepository newInstance(String repo) {
        log.info("【工厂实例】:"+verifyCodeMap.toString());
        VerificationCodeRepository verificationCodeRepository = verifyCodeMap.get(repo.toLowerCase());
        return verificationCodeRepository;
    }
}
